#include "musicslider.h"
#include "ui_musicslider.h"
#include<QMouseEvent>

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    maxWidth=width();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::moveSlider()
{
    ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(),currentPos,ui->outLine->height());
}

void MusicSlider::setStep(float bf)
{
    currentPos=bf*maxWidth;
    moveSlider();
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    (void)event;
    currentPos=event->pos().x();
    moveSlider();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    (void)event;
    QRect rect=QRect(0,0,this->width(),this->height());
    QPoint point=event->pos();
    if(!rect.contains(point))return;
    if(event->buttons()==Qt::LeftButton)
    {
        currentPos=event->pos().x();
        if(currentPos<0)currentPos=0;
        else if(currentPos>maxWidth)currentPos=maxWidth;
        moveSlider();
    }
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    currentPos=event->pos().x();
    moveSlider();
    emit setMusicSliderPosition((float)currentPos/(float)maxWidth);
}


//QFrame???
